package com.northwest_man.refactor_tool;


import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CompareStringXml {// private static final String[] dirs = new String[]{"values", "values-de", "values-el", "values-es", "values-fr",
//         "values-it", "values-nl", "values-pl", "values-pt", "values-ru", "values-sv", "values-tr"};
        private static final String[] dirs = new String[]{"values"};

    public static void main() throws IOException{

        for (final String dir : dirs) {
            final String newFileAbs = String.format("%s%s%s%s%s",Constants. HOME_DIR ,"/Desktop/AndroidStringFiles" ,"/", dir,"/strings.xml" );
            final String oldFileAbs = String.format("%s%s%s%s%s",Constants. PAT_PROJECT_DIR ,"/app/src/main/res" ,"/", dir,"/strings.xml" );
            final StringBuilder newFileContent  =new StringBuilder(FileUtils.readFileToString(new File(newFileAbs)));
            final StringBuilder oldFileContent  = new StringBuilder(FileUtils.readFileToString(new File(oldFileAbs)));
            // <string\s+name\s*=\s*"([^"]+?)"\s*>([\s\S]+?)</\s*string\s*>
            final Pattern newPattern = Pattern.compile("<string\\s+name\\s*=\\s*\"([^\"]+?)\"\\s*>([\\s\\S]+?)</\\s*string\\s*>");
            final Matcher newMatcher = newPattern.matcher(newFileContent);
         // 新增的
            final StringBuilder newStringList = new StringBuilder();
            while(newMatcher.find()) {
                final String key = newMatcher.group(1);
                final String newValue = newMatcher.group(2);
                final Pattern oldPattern = Pattern.compile(String.format("<string\\s+name\\s*=\\s*\"%s\"\\s*>([\\s\\S]+?)</\\s*string\\s*>", key));
                final Matcher oldMatcher = oldPattern.matcher(oldFileContent);
                // 在现有的 strings.xml 文件中没找到该 key
                if (oldMatcher.find()){
                    final String oldValue = oldMatcher.group(1);
                    final int start = oldMatcher.start();
                    final int end = oldMatcher.end();
                    if (!newValue .equals(oldValue)) {
                        System.out.println(String.format("不同，找到 key(%s)，老(%s)，新(%s)", key, oldValue, newValue));
                        oldFileContent .replace(start, end, newMatcher.group(0));
                    } else {
                        // System.out.println(String.format("相同，找到 key(%s)，老(%s)，新(%s)", key, oldValue, newValue));
                    }
                } else {
                    System.out.println(String.format("没找到 key(%s)", key));
                    newStringList.append("\n").append(newMatcher.group(0));
                }
               
            }
            if (newStringList.length() >0){
                final Pattern endPattern = Pattern.compile("</\\s*resources\\s*>");
                final Matcher endMatcher = endPattern.matcher(oldFileContent);
                if (endMatcher.find()) {
                    
                    oldFileContent.insert(endMatcher.start() -1,newStringList );
                } else {
                    throw new RuntimeException();
                }
            }
            FileUtils.writeStringToFile(new File(oldFileAbs), oldFileContent.toString(), false);
        }
    }
}
